<?php

namespace App\Services;

use App\Models\DossierCombination;
use App\Models\DossierTemplate;
use Illuminate\Database\Eloquent\Model;

/**
 * 案卷构成规范
 * Class DossierCombinationService
 * @package App\Services
 */
class DossierCombinationService extends BaseService
{
    /**
     * 新增一个案卷构成规范
     */
    public function saveData($params)
    {
        if ($params['id']) {
            $dossierCombination = DossierCombination::query()->find($params['id']);
            $content = '编辑案卷构成规范';
        } else {
            $dossierCombination = new DossierCombination();
            $content = '新增案卷构成规范';
        }
        $dossierCombination->title           = $params['title'];
        $dossierCombination->book_title_id   = $params['book_title_id'] ?? 0; //案卷目录
        $dossierCombination->book_catalog_id = $params['book_catalog_id'] ?? 0; //卷内目录
        $dossierCombination->appendix_id     = $params['appendix_id'] ?? 0; //备考表
        $dossierCombination->cover_id        = $params['cover_id'] ?? 0; //封面
        $dossierCombination->catalogue_id    = $params['catalogue_id'] ?? 0; //全引目录

        $result = $dossierCombination->save();
        CommonService::addActionLog($content, "数据ID:{$dossierCombination->id}");

        return $result;
    }

    /**
     * 获取一条信息
     */
    public function getTemplates()
    {
        $list = DossierTemplate::query()->select('id', 'title', 'type')->get()->toArray();
        $covers = $appendixes = $bookTitles = $bookCatalogs = $catalogues = [];
        foreach ($list as $val) {
            if ($val['type'] == 1) {
                $covers[] = $val;
            }
            if ($val['type'] == 2) {
                $appendixes[] = $val;
            }
            if ($val['type'] == 3) {
                $bookTitles[] = $val;
            }
            if ($val['type'] == 4) {
                $bookCatalogs[] = $val;
            }
            if ($val['type'] == 5) {
                $catalogues[] = $val;
            }
        }
        return [
            'covers'        => $covers,
            'appendixes'    => $appendixes,
            'book_titles'   => $bookTitles,
            'book_catalogs' => $bookCatalogs,
            'catalogues'    => $catalogues,
        ];
    }

    /**
     * 编辑一个案卷构成规范
     */
    public function updateData($id, $params)
    {
        $dossierCombination = DossierCombination::query()->where('id', $id)->first();

        $dossierCombination->title           = $params['title'];
        $dossierCombination->cover_id        = $params['cover_id']; //封面
        $dossierCombination->appendix_id     = $params['appendix_id']; //备考表
        $dossierCombination->book_title_id   = $params['book_title_id']; //案卷目录
        $dossierCombination->book_catalog_id = $params['book_catalog_id']; //卷内目录
        $dossierCombination->catalogue_id    = $params['catalogue_id']; //全引目录
        $dossierCombination->sort            = $params['sort'];

        CommonService::addActionLog('编辑一个案卷构成规范', "数据ID:{$id}");

        return $dossierCombination->save();
    }

    /**
     * 案卷构成组合列表
     */
    public function getList($perPage, $keyword): array
    {
        $list = DossierCombination::query()
            ->when($keyword, function ($query) use ($keyword) {
                $query->where('title', 'like', "%{$keyword}%");
            })
            ->paginate($perPage)
            ->toArray();
        $noImg = config('site.no_img');
        foreach ($list['data'] as &$val) {
            $val['book_title_img']   = DossierTemplate::query()->where('id', $val['book_title_id'])->value('thumb') ?? $noImg;
            $val['cover_img']        = DossierTemplate::query()->where('id', $val['cover_id'])->value('thumb') ?? $noImg;
            $val['appendix_img']     = DossierTemplate::query()->where('id', $val['appendix_id'])->value('thumb') ?? $noImg;
            $val['book_catalog_img'] = DossierTemplate::query()->where('id', $val['book_catalog_id'])->value('thumb') ?? $noImg;
            $val['catalogue_img']    = DossierTemplate::query()->where('id', $val['catalogue_id'])->value('thumb') ?? $noImg;
        }

        return $this->removeNoUsedPageParams($list);
    }

    /**
     * 获取记录列表（不分页），下拉选择用
     */
    public function getCombinations()
    {
        $list = DossierCombination::query()->select('id', 'title')->get();

        return $list->isNotEmpty() ? $list->toArray() : [];
    }

    /**
     * 获取回收站数据列表
     */
    public function getRecycles($request): array
    {
        $thisQuery = DossierCombination::onlyTrashed();

        if ($request->filled('keyword')) { //判断参数是否存在并且不为空
            $keyword = $request->input('keyword');
            $thisQuery->where('title', 'like', "%{$keyword}%"); //由于不同的表字段不同，所以单独拿出来
        }

        return $this->formatRecycleData($request, $thisQuery);
    }

}
